package com.example.baweyplayer.db;

import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.musicbrainz.android.api.data.ReleaseArtist;
import org.musicbrainz.android.api.data.ReleaseGroup;
import org.musicbrainz.android.api.data.ReleaseGroupInfo;
import org.musicbrainz.android.api.data.ReleaseInfo;

import android.util.Log;

import com.j256.ormlite.field.DatabaseField;

public class DbRelease {

	public final static String COL_NAME_RELEASE_GROUP_MBID = "groupMbid";
	public final static String COL_NAME_RELEASE_MBID = "individualMbid";
	public final static String COL_NAME_TITLE = "title";
	public final static String COL_NAME_ARTIST = "artist";
	public final static String COL_NAME_RELEASE_YEAR = "releaseYear";
	public final static String COL_NAME_IMAGE_PATH = "imagePath";

	public final static Pattern yearPattern = Pattern.compile("(\\d{4})");

	@DatabaseField(generatedId = true)
	int id;

	@DatabaseField(index = true, unique = true, columnName = COL_NAME_RELEASE_GROUP_MBID)
	String groupMbid;

	@DatabaseField(index = true, unique = true, columnName = COL_NAME_RELEASE_MBID)
	String individualMbid;

	@DatabaseField(index = true, columnName = COL_NAME_TITLE)
	String title;

	@DatabaseField(index = true, foreign = true, foreignColumnName = DbArtist.COL_NAME_MBID, columnName = COL_NAME_ARTIST)
	DbArtist artist;

	@DatabaseField(columnName = COL_NAME_RELEASE_YEAR)
	int releaseYear;

	@DatabaseField(columnName = COL_NAME_IMAGE_PATH)
	String imagePath;

	public DbRelease() {
	}

	public DbRelease(ReleaseGroup rg) {
		groupMbid = rg.getMbid();
		title = rg.getTitle();
		if (rg.getFirstRelease() != null) {
			releaseYear = (short) rg.getFirstRelease().get(Calendar.YEAR);
		}
	}

	public DbRelease(ReleaseGroupInfo rgInfo) {
		groupMbid = rgInfo.getMbid();
		title = rgInfo.getTitle();
		List<ReleaseArtist> artists = rgInfo.getArtists();
		if (artists.size() > 0) {
			Log.d(this.getClass().getName(), "album has some artist: " + artists.get(0).getName());
		}

		// TODO: this info is unfortunately worthless
		// if (rgInfo.getFirstRelease() != null) {
		// releaseYear = (short) rgInfo.getFirstRelease().get(Calendar.YEAR);
		// }
	}

	/** Data manipulation... **/

	public String getGroupMbid() {
		return groupMbid;
	}

	public void setGroupMbid(String groupMbid) {
		this.groupMbid = groupMbid;
	}

	public String getIndividualMbid() {
		return individualMbid;
	}

	public void setIndividualMbid(String individualMbid) {
		this.individualMbid = individualMbid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public DbArtist getArtist() {
		return artist;
	}

	public void setArtist(DbArtist artist) {
		this.artist = artist;
	}

	public int getReleaseYear() {
		return releaseYear;
	}

	/**
	 * Safe - matches the string against a pattern
	 * 
	 * @param releaseYear
	 */
	public void setReleaseYear(String releaseYear) {
		if (releaseYear != null) {
			Matcher yearMatcher = yearPattern.matcher(releaseYear);
			if (yearMatcher.find()) {
				this.releaseYear = Integer.parseInt(yearMatcher.group());
			}
		}
	}

	public void setReleaseYear(int year) {
		this.releaseYear = year;
	}

	public int getId() {
		return id;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	public void setReleaseYearBasedOnReleases(List<ReleaseInfo> releases, boolean instantSave) {
		int minYear = 3000;
		for (ReleaseInfo release : releases) {
			if (release.getDate() != null) {
				Matcher yearMatcher = yearPattern.matcher(release.getDate());
				if (yearMatcher.find()) {
					minYear = Math.min(Integer.parseInt(yearMatcher.group()), minYear);
				}
			}
		}
		if (minYear < 3000) {
			setReleaseYear(minYear);
			if (instantSave) {
				DatabaseHelper.getInstance().getReleaseRed().update(this);
			}
		}
	}

	@Override
	public String toString() {
		return "DbRelease [groupMbid=" + groupMbid + ", title=" + title + ", artist=" + artist + ", releaseYear=" + releaseYear + ", imagePath="
				+ imagePath + "]";
	}

}
